/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @FraMyBa.java  2014年9月11日 上午10:08:57 - Guzhu
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.fragment;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaListFragment;
import com.android.tiango.baba.act.ActHome;
import com.android.tiango.baba.act.ActSearch;
import com.android.tiango.baba.adapter.MybaJoinAdapter;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.cookie.ShareCookie;
import com.android.tiango.baba.entity.BarEntity;
import com.android.tiango.baba.entity.BarModel;
import com.android.tiango.baba.entity.BaseEntity;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.ImageLoader;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.ToastUtils;
import com.android.tiango.baba.view.MybaListView;
import com.android.tiango.baba.view.PullListView;
import com.android.tiango.baba.view.PullListView.PullListViewListener;

/**
 * 我的吧
 * @author givon
 * 
 */
public class FraMyBa extends BaListFragment<BarModel> implements
		com.android.tiango.baba.BaListFragment.AddHeaderViewCallBack, PullListViewListener {
	private Button mSearchButton;
	private RadioButton mRbQgRadioButton;
	private RadioButton mRbMyRadioButton;
	private View mHeaderView;
	private MybaListView mHeaderListView;
	private TextView mJoinNum;
	private TextView mRecomdTextView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		setHeaderViewCallBack(this);
		// View view = super.onCreateView(inflater, container, savedInstanceState);
		System.out.println("onCreateView");
		View v = inflater.inflate(R.layout.layout_myba, container, false);
		RadioGroup mRadioGroup = (RadioGroup) v.findViewById(R.id.rg_T);
		mRbMyRadioButton = (RadioButton) v.findViewById(R.id.rb_myba);
		mRbQgRadioButton = (RadioButton) v.findViewById(R.id.rb_qianggouba);
		mRbQgRadioButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (ActHome.WEI_BA_TYPE == ActHome.MY_BA) {
					((ActHome) getActivity()).switchFragment(ActHome.QIANG_GOU_BA);
				}
			}
		});
		mAdapter = new ListAdapter(getActivity());
		mListView = (PullListView) v.findViewById(R.id.id_pull_listview);
		mListView.setHeaderDividersEnabled(false);
		mListView.setFooterDividersEnabled(false);
		mListView.setSelector(android.R.color.transparent);
		if (null != addHeaderViewCallBack) {
			addHeaderViewCallBack.addHeaderView();
		}
		mSearchButton = (Button) v.findViewById(R.id.bt_serach);
		mSearchButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showActivity(ActSearch.class);
			}
		});
		mImageLoader = new ImageLoader(getActivity(), R.drawable.ic_default);
		mListView.setAdapter(mAdapter);
		mListView.setPullListener(this);
		mListView.startOnRefresh();
		return v;
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	};

	@Override
	public void onStart() {
		super.onStart();
		mRbMyRadioButton.setChecked(true);
	}

	@Override
	public void onPullRefresh() {
		mPageIndex = 1;
		loadJoinData();
		loadCommendData();
	}

	@Override
	public void onPullLoadMore() {
		mPageIndex += 1;
		loadJoinData();
		loadCommendData();
	}

	@Override
	public void addHeaderView() {
		mHeaderView = LayoutInflater.from(getActivity()).inflate(R.layout.header_fragment_myba,
				null);
		mHeaderListView = (MybaListView) mHeaderView.findViewById(R.id.lv_join_list);
		mJoinNum = (TextView) mHeaderView.findViewById(R.id.tv_join_num);
		mRecomdTextView = (TextView) mHeaderView.findViewById(R.id.tv_recomd);
		mListView.addHeaderView(mHeaderView);
	}

	@Override
	protected View getItemView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(getActivity()).inflate(R.layout.item_myba_join, null);
			viewHolder = new ViewHolder();
			viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
			viewHolder.tv_chengYuan = (TextView) convertView.findViewById(R.id.tv_chengYuan);
			viewHolder.tv_huati = (TextView) convertView.findViewById(R.id.tv_huati);
			viewHolder.bt_join = (Button) convertView.findViewById(R.id.bt_join);
			viewHolder.iv_header = (ImageView) convertView.findViewById(R.id.iv_header);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final BarModel bean = mListData.get(position);
		if (null != bean) {
			viewHolder.tv_title.setText(bean.getBarName());
			viewHolder.tv_chengYuan.setText(bean.getMemberNumber() + "");
			viewHolder.tv_huati.setText(bean.getTodayTopicNumber() + "");
			viewHolder.bt_join.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					addBar(bean.getBarId());
				}
			});
			mImageLoader.loadImage(bean.getBarIcon(), viewHolder.iv_header);
		}

		return convertView;
	}

	private class ViewHolder {
		private TextView tv_title;
		private TextView tv_chengYuan;
		private TextView tv_huati;
		private Button bt_join;
		private ImageView iv_header;

	}

	// 我加入的吧
	private void loadJoinData() {
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams param = new HttpParams();
		param.put("token", ShareCookie.getToken());
		client.get(HttpUrl.getUrl(HttpUrl.MyFocusBars), param, new HttpCallBack() {

			@Override
			public void onHttpSuccess(Object arg0) {
				BarEntity entity = (BarEntity) arg0;
				ArrayList<BarModel> list = entity.getData();
				if (null != list && list.size() > 0) {
					mJoinNum.setVisibility(View.VISIBLE);
					mJoinNum.setText("我加入的吧(" + list.size() + ")");
					MybaJoinAdapter adapter = new MybaJoinAdapter(getActivity(), list);
					adapter.setImageLoader(mImageLoader);
					mHeaderListView.setAdapter(adapter);
				} else {
					mJoinNum.setVisibility(View.GONE);
				}
			}

			@Override
			public void onHttpStarted() {

			}

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				if (!StringUtils.isEmpty(arg1)) {
					ToastUtils.showMessage(arg1);
				}
			}
		}, BarEntity.class);//

		// ArrayList<PanicBuyGoodsBean> list = new ArrayList<PanicBuyGoodsBean>();
		// for (int i = 0; i < 10; i++) {
		// list.add(new PanicBuyGoodsBean());
		// }
		// mAdapter.putData(list);
		// MybaJoinAdapter adapter = new MybaJoinAdapter(getActivity(), list);
		// mHeaderListView.setAdapter(adapter);
		// mListView.onRefreshFinish();
		// mListView.onLoadFinish();
	}

	// 热门推荐
	private void loadCommendData() {
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams param = new HttpParams();
		param.put("token", ShareCookie.getToken());
		param.put("pageIndex", mPageIndex);
		param.put("pageSize", Constants.PAGE_SIZE);

		client.get(HttpUrl.getUrl(HttpUrl.RecommendBars), param, new HttpCallBack() {

			@Override
			public void onHttpSuccess(Object arg0) {
				BarEntity entity = (BarEntity) arg0;
				ArrayList<BarModel> list = entity.getData();
				if (null != list) {
					mAdapter.putData(list);
				}
				mListView.onRefreshFinish();
				mListView.onLoadFinish();

			}

			@Override
			public void onHttpStarted() {

			}

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				mListView.onRefreshFinish();
				mListView.onLoadFinish();
				if (!StringUtils.isEmpty(arg1)) {
					ToastUtils.showMessage(arg1);
				}
			}
		}, BarEntity.class);//
	}

	// 加入吧
	private void addBar(String barId) {
		showWaitingDialog();
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams param = new HttpParams();
		param.put("barId", barId);
		param.put("token", ShareCookie.getToken());
		param.put("type", "1");// 0-退出 1-加入

		client.post(HttpUrl.getUrl(HttpUrl.OperateTopicBar), param, new HttpCallBack() {

			@Override
			public void onHttpSuccess(Object arg0) {
				dismissWaitingDialog();
				BaseEntity entity = (BaseEntity) arg0;
				if (null != entity) {
					ToastUtils.showMessage("加入成功");
				}
			}

			@Override
			public void onHttpStarted() {

			}

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				if (!StringUtils.isEmpty(arg1)) {
					ToastUtils.showMessage(arg1);
				}
				dismissWaitingDialog();
			}
		}, BaseEntity.class);//
	}
}
